package com.tangsm.spring.boot.leaf.service;

import com.sankuai.inf.leaf.common.Result;
import com.sankuai.inf.leaf.service.LeafSegmentService;
import com.sankuai.inf.leaf.service.LeafSnowflakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class IdGeneratorService {

    @Autowired
    @Qualifier("segmentIdGenerator")
    private LeafSegmentService segmentIdGenerator;

    @Autowired
    @Qualifier("snowflakeIdGenerator")
    private LeafSnowflakeService snowflakeIdGenerator;

    /**
     * 使用号段模式生成ID
     */
    public Long generateSegmentId(String bizTag) {
        Result result = segmentIdGenerator.getId(bizTag);
        if (result.getStatus().equals(Result.Status.SUCCESS)) {
            return result.getId();
        } else {
            throw new RuntimeException("生成号段ID失败: " + result.toString());
        }
    }

    /**
     * 使用雪花算法生成ID
     */
    public Long generateSnowflakeId() {
        Result result = snowflakeIdGenerator.getId("snowflake");
        if (result.getStatus().equals(Result.Status.SUCCESS)) {
            return result.getId();
        } else {
            throw new RuntimeException("生成雪花ID失败: " + result.toString());
        }
    }
}
